/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

 
function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (ex) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}

var xmlhttp = new getXMLObject();	//xmlhttp holds the ajax object
var xmlhttpcategory = new getXMLObject();
var xmlhttpmovie = new getXMLObject();
var xmlhttpmovie2 = new getXMLObject(); // hotmovie
var xmlhttpmovie3 = new getXMLObject(); // newestmovie
var xmlhttpmovie4 = new getXMLObject(); // randommovie


function retrieveCategories(){
    document.getElementById("indexcategoryloader").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='50'  /></div>";
    if(xmlhttpcategory) {
      
            xmlhttpcategory.open("GET","RetrieveCategories",true); //servlet name
            xmlhttpcategory.onreadystatechange  = handleServerResponseForBrowse;
            xmlhttpcategory.send();
    }
}

function handleServerResponseForBrowse(){
     if (xmlhttpcategory.readyState==4 && xmlhttpcategory.status==200)
     {
            document.getElementById("indexcategorylist").innerHTML=xmlhttpcategory.responseText;
     }
    else if(xmlhttpcategory.readyState==4 && xmlhttpcategory.status==404){
            xmlhttpcategory.getElementById("indexcategorylist").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
}

function loadingBarForBrowse(){
 //   document.getElementById("categorypanelist").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='180'  /></div>";
    document.getElementById("categorypaneloader").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='180'  /></div>";

}



function submitRegistration(){
       
        var curdate = new Date();
        var password = document.getElementById("password").value;;
        var username = document.getElementById("username").value;
        var firstName = document.getElementById("firstname").value;
        var lastName = document.getElementById("lastname").value;
        var gender = document.getElementsByName("gender");
        var address = document.getElementById("address").value;
        var birthday;
        var email = document.getElementById("email").value;
        var joindate = curdate.getFullYear()+"-"+curdate.getMonth()+"-"+curdate.getDate();
        var jointime = curdate.getHours()+":"+curdate.getMinutes()+":"+curdate.getSeconds();
        var age = parseInt(curdate.getFullYear()) - parseInt($("select#Year option:selected").text());
	var terms = (document.getElementById("terms").checked) ? 1 : 0;
                
	if($("select#Year option:selected").text() != "Year:" && $("select#Month option:selected").text() != "Month:" && $("select#Day option:selected").text() != "Day:")
            birthday = $("select#Year option:selected").text()+"-"+$("select#Month option:selected").attr('value')+"-"+$("select#Day option:selected").text();
                
        if(curdate.getMonth()<$("select#Month option:selected").attr('value'))
            age--;
	else if(curdate.getMonth()==$("select#Month option:selected").text())
            if(curdate.getDate()>$("select#Day option:selected").text())
                age--;            
            
        var gendernotifier =0;
        for (var genderindex = 0; genderindex < gender.length; genderindex ++) {
             if (gender[genderindex ].checked) {
                gender = gender[genderindex].value;
                gendernotifier=1;
                break;
             }
       }
          
        
        if(!username || !password || !birthday || !address || !email || !firstName || !lastName || !gendernotifier || !terms){
            document.getElementById("registerindicator").innerHTML = "Please fill out all the forms!";
            
        }
        else{
        
        if(xmlhttp){
            
            document.getElementById("registerindicator").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='180'  /></div>";
            xmlhttp.open("POST","RegisterUser",true); //servlet name
            xmlhttp.onreadystatechange  = handleServerResponseForRegister;
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
            xmlhttp.send("username="+username+"&password="+password+"&firstName="+firstName+"&lastName="+lastName+"&age="+age+"&gender="+gender+"&address="+address+"&birthday="+birthday+"&email="+email+"&joindate="+joindate+"&jointime="+jointime);
        }
        }
        
        

        
}

function handleServerResponseForRegister(){
    
    document.getElementById("registerpane").innerHTML=xmlhttp.responseText;
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
            document.getElementById("registerpane").innerHTML=xmlhttp.responseText;
           
     }
    else if(xmlhttp.readyState==4 && xmlhttp.status==404){
            pageNotFoundForRegister();
     }
}

function loginUser(idmovie,idcategory){
    var username = document.getElementById("username_login").value;
    var password = document.getElementById("password_login").value;
    
    if(!username || !password)
       document.getElementById("loginindicator").innerHTML = "Fill out all forms";
    else{
    if(xmlhttp) {
            loadingBarForLogin();
            xmlhttp.open("POST","Login?idmovie="+idmovie+"&idcategory="+idcategory,true); //servlet name
            xmlhttp.onreadystatechange  = handleServerResponseForLogin;
            xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttp.send("username="+username+"&password="+password);
    }
    }
}

function handleServerResponseForLogin(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
            document.getElementById("realbody").innerHTML=xmlhttp.responseText;
           
     }
    else if(xmlhttp.readyState==4 && xmlhttp.status==404){
            pageNotFoundForRegister();
     }
    
}

function loadingBarForLogin(){
    document.getElementById("loadersignin").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='180'  /></div>";
}


function retrieveUpcomingMovie(){
    if(xmlhttpmovie) {
            document.getElementById("indexupcomingmovieloader").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='180'  /></div>";
            xmlhttpmovie.open("GET","RetrieveNearestUpcomingMovie",true); //servlet name
            xmlhttpmovie.onreadystatechange  = handleServerResponseForUpcomingMovie;
            xmlhttpmovie.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpmovie.send();
    }
    
}
    
function handleServerResponseForUpcomingMovie(){
    if (xmlhttpmovie.readyState==4 && xmlhttpmovie.status==200)
     {
            document.getElementById("indexupcomingmovie").innerHTML=xmlhttpmovie.responseText;
           
     }
    else if(xmlhttpmovie.readyState==4 && xmlhttpmovie.status==404){
            pageNotFoundForRegister();
     }
    
}

function checkUsernameAvailability(){
    username = document.getElementById("username").value;
    
    if(username){
       
    if(xmlhttp) {
            
            
            document.getElementById("checkuseravailabilityerror").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='30px'  /></div><br>";
            xmlhttp.open("GET","CheckUsernameAvailability?username="+username,true); //servlet name
            xmlhttp.onreadystatechange  = handleServerResponseForUsernameAvailability;
            xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttp.send();
    }
    }
}
    
function handleServerResponseForUsernameAvailability(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
            document.getElementById("checkuseravailabilityerror").innerHTML=xmlhttp.responseText;
           
     }
    else if(xmlhttp.readyState==4 && xmlhttp.status==404){
            pageNotFoundForRegister();
     }
    
}


//WORK THIS OUT!!

function chosenCategory(name){
    
    document.getElementById("categorymoviespane").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='180'  /></div>";
    
    
    if(xmlhttpmovie) {
      
            xmlhttpmovie.open("GET","RetrieveNewMoviesByCategory?name="+name,true); //servlet name
            xmlhttpmovie.onreadystatechange  = handleServerResponseForChosenCategory;
            xmlhttpmovie.send();
    }
}

function handleServerResponseForChosenCategory(){
     if (xmlhttpmovie.readyState==4 && xmlhttpmovie.status==200)
     {
            document.getElementById("categorymoviespane").innerHTML=xmlhttpmovie.responseText;
     }
    else if(xmlhttpmovie.readyState==4 && xmlhttpmovie.status==404){
            xmlhttpmovie.getElementById("categorymoviespane").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
}

///////////////////


function retrieveTop50Movies(){
      
 
      
    if(xmlhttpmovie) {
            
            
            document.getElementById("top50pane").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='30px'  /></div><br>";
            xmlhttpmovie.open("GET","RetrieveTop50Movies",true); //servlet name
            xmlhttpmovie.onreadystatechange  = handleServerResponseForTop50Movies;
            xmlhttpmovie.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpmovie.send();
    }
    
}
    
function handleServerResponseForTop50Movies(){
    if (xmlhttpmovie.readyState==4 && xmlhttpmovie.status==200)
     {
            document.getElementById("top50pane").innerHTML=xmlhttpmovie.responseText;
           
     }
    else if(xmlhttpmovie.readyState==4 && xmlhttpmovie.status==404){
            pageNotFoundForRegister();
     }
    
}


function retrieveUpcomingMovies(){
      
     
    if(xmlhttpmovie) {
            
            
            document.getElementById("upcomingreleasespane").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='30px'  /></div><br>";
            xmlhttpmovie.open("GET","RetrieveUpcomingMovies",true); //servlet name
            xmlhttpmovie.onreadystatechange  = handleServerResponseForUpcomingMovies;
            xmlhttpmovie.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpmovie.send();
    }
    
}
    
function handleServerResponseForUpcomingMovies(){
    if (xmlhttpmovie.readyState==4 && xmlhttpmovie.status==200)
     {
            document.getElementById("upcomingreleasespane").innerHTML=xmlhttpmovie.responseText;
           
     }
    else if(xmlhttpmovie.readyState==4 && xmlhttpmovie.status==404){
            pageNotFoundForRegister();
     }
    
}


function retrieveNewReleases(){
      
     
    if(xmlhttpmovie) {
            
            
            document.getElementById("newreleasespane").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='30px'  /></div><br>";
            xmlhttpmovie.open("GET","RetrieveNewReleases",true); //servlet name
            xmlhttpmovie.onreadystatechange  = handleServerResponseForNewReleases;
            xmlhttpmovie.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpmovie.send();
    }
    
}
    
function handleServerResponseForNewReleases(){
    if (xmlhttpmovie.readyState==4 && xmlhttpmovie.status==200)
     {
            document.getElementById("newreleasespane").innerHTML=xmlhttpmovie.responseText;
           
     }
    else if(xmlhttpmovie.readyState==4 && xmlhttpmovie.status==404){
            pageNotFoundForRegister();
     }
    
}

function retrieveMovieById(id){
 
    
    if(xmlhttpmovie) {
            
            
            document.getElementById("moviepane").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='30px'  /></div><br>";
            xmlhttpmovie.open("GET","RetrieveMovieById?id="+id,true); //servlet name
            xmlhttpmovie.onreadystatechange  = handleServerResponseForMovieById;
            xmlhttpmovie.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpmovie.send();
    }
    
}
    
function handleServerResponseForMovieById(){
   
    if (xmlhttpmovie.readyState==4 && xmlhttpmovie.status==200)
     {
            document.getElementById("moviepane").innerHTML=xmlhttpmovie.responseText;
           
     }
    else if(xmlhttpmovie.readyState==4 && xmlhttpmovie.status==404){
            pageNotFoundForRegister();
     }
    
}


function retrieveStarClick(num,id){
 
    
    if(xmlhttpmovie) {
            
            
            document.getElementById("starloader").innerHTML="<div style='text-align: center;'><img src='images/loader.gif' alt='Loading' align='left' width='30px'  /></div><br><br>";
            xmlhttpmovie.open("GET","AddRating?id="+id+"&num="+num,true); //servlet name
            xmlhttpmovie.onreadystatechange  = handleServerResponseForStarClick;
            xmlhttpmovie.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpmovie.send();
    }
    
}
    
function handleServerResponseForStarClick(){
    
    if (xmlhttpmovie.readyState==4 && xmlhttpmovie.status==200)
     {
            document.getElementById("ratingdiv").innerHTML=xmlhttpmovie.responseText;
           
     }
    else if(xmlhttpmovie.readyState==4 && xmlhttpmovie.status==404){
            pageNotFoundForRegister();
     }
    
}

function retrieveHotMovie(){
    document.getElementById("hotmovie").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='50'  /></div>";
    if(xmlhttpmovie2) {
     
            xmlhttpmovie2.open("GET","RetrieveHotMovie",true); //servlet name
            xmlhttpmovie2.onreadystatechange  = handleServerResponseForHotMovie;
            xmlhttpmovie2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpmovie2.send();
    }
}

function handleServerResponseForHotMovie(){
     
     
     if (xmlhttpmovie2.readyState==4 && xmlhttpmovie2.status==200)
     {
            document.getElementById("hotmovie").innerHTML=xmlhttpmovie2.responseText;
     }
    else if(xmlhttpmovie2.readyState==4 && xmlhttpmovie2.status==404){
            xmlhttpmovie2.getElementById("hotmovie").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
}

function retrieveNewestMovie(){
    
    document.getElementById("newestrelease").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='50'  /></div>";
    
    if(xmlhttpmovie3) {
     
            xmlhttpmovie3.open("GET","RetrieveNewestMovie",true); //servlet name
            xmlhttpmovie3.onreadystatechange  = handleServerResponseForNewestMovie;
            xmlhttpmovie3.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpmovie3.send();
    }
}

function handleServerResponseForNewestMovie(){
     
     if (xmlhttpmovie3.readyState==4 && xmlhttpmovie3.status==200)
     {
            document.getElementById("newestrelease").innerHTML=xmlhttpmovie3.responseText;
     }
    else if(xmlhttpmovie3.readyState==4 && xmlhttpmovie3.status==404){
            xmlhttpmovie3.getElementById("newestrelease").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
}

function retrieveRandomMovie(){
    
    document.getElementById("randommoviepane").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='50'  /></div>";
    
    if(xmlhttpmovie4) {
     
            xmlhttpmovie4.open("GET","RetrieveRandomMovie",true); //servlet name
            xmlhttpmovie4.onreadystatechange  = handleServerResponseForRandomMovie;
            xmlhttpmovie4.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpmovie4.send();
    }
}

function handleServerResponseForRandomMovie(){
     document.getElementById("randommoviepane").innerHTML=xmlhttpmovie4.responseText;
     if (xmlhttpmovie4.readyState==4 && xmlhttpmovie4.status==200)
     {
            document.getElementById("randommoviepane").innerHTML=xmlhttpmovie4.responseText;
     }
    else if(xmlhttpmovie4.readyState==4 && xmlhttpmovie4.status==404){
            xmlhttpmovie4.getElementById("randommoviepane").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
}


function addToCart(idmovie,iduser){
   
    document.getElementById("modalpopupsubmit").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='50'  /></div>";
    
    if(xmlhttp) {
     
            xmlhttp.open("GET","AddToCart?idmovie="+idmovie+"&iduser="+iduser,true); //servlet name
            xmlhttp.onreadystatechange  = handleServerResponseToCart;
            xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttp.send();
    }
}

function handleServerResponseToCart(){
     
    
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {     alert("Movie successfully added to cart!")
         
            
            document.getElementById("modalpopupsubmit").innerHTML ="<input type='submit' value='Confirm' style='padding:7px;width:100%;'>";
            hidePopUp();
     }
    else if(xmlhttp.readyState==4 && xmlhttp.status==404){
            xmlhttp.getElementById("modalpopupsubmit").innerHTML="<h3>Page Not Found. Please Try Again</h3>";
     }
}


function retrieveCart(id){
    
    document.getElementById("cartpane").innerHTML="<div style='text-align: left;'><img src='images/loader.gif' alt='Loading' align='left' width='30px'  /></div><br>";
    
    if(xmlhttp) {
            
            
            
            xmlhttp.open("GET","RetrieveCart?id="+id,true); //servlet name
            xmlhttp.onreadystatechange  = handleServerResponseForCart;
            xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttp.send();
    }
    
}
    
function handleServerResponseForCart(){
    
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
            document.getElementById("cartpane").innerHTML=xmlhttp.responseText;
           
     }
    else if(xmlhttp.readyState==4 && xmlhttp.status==404){
            pageNotFoundForRegister();
     }
    
}
